#include "readywork.h"
#include "ui_readywork.h"

ReadyWork::ReadyWork(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ReadyWork)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    keyboard=new Keyboard(this);
    ui->lblHelp->setText(tr("帮助:\n")+tr(READWORK_HELP));
    on_btnCancel_clicked();
}

ReadyWork::~ReadyWork()
{
    if(keyboard)
        delete keyboard;
    delete ui;
}

void ReadyWork::on_btnTemper_clicked()
{
    keyboard->RunKeyboard(Keyboard::DATA_TH);
    disconnect(keyboard,0,0,0);
    connect(keyboard,SIGNAL(btnKey_OK_clicked(float)),this,SLOT(set_temper(float)));
}

void ReadyWork::on_btnHumi_clicked()
{
    keyboard->RunKeyboard(Keyboard::DATA_TH);
    disconnect(keyboard,0,0,0);
    connect(keyboard,SIGNAL(btnKey_OK_clicked(float)),this,SLOT(set_humi(float)));
}

void ReadyWork::on_btnTime_clicked()
{
    keyboard->RunKeyboard(Keyboard::DATA_WT);
    disconnect(keyboard,0,0,0);
    connect(keyboard,SIGNAL(btnKey_OK_clicked(U64)),this,SLOT(set_worktime(U64)));
}

void ReadyWork::on_btnCancel_clicked()
{
    temper=0;
    humi=0;
    worktime=0;
    ui->btnTemper->setText(QString::number(temper)+tr("℃"));
    ui->btnHumi->setText(QString::number(humi)+tr("%"));
    ui->btnTime->setText(QString::number(worktime)+tr("分钟"));
    hide();
}

void ReadyWork::on_btnOK_clicked()
{
    QMessageBox msgBox;
    int ret;
    msgBox.setIconPixmap(QPixmap(QString(":/icos/res/ico/msgbox_warning.png")));
    msgBox.addButton(tr("确定"),QMessageBox::AcceptRole);
    msgBox.addButton(tr("取消"),QMessageBox::RejectRole);
    if(worktime==0)
        msgBox.setText(tr("设定的工作温度为:")+QString::number(temper)+tr("℃\n")
                                                                                        +tr("设定工作湿度为:")+QString::number(humi)+tr("%"));
    else
        msgBox.setText(tr("设定的工作温度为:")+QString::number(temper)+tr("℃\n")
                                                                                        +tr("设定工作湿度为:")+QString::number(humi)+tr("%\n")
                                                                                        +tr("任务时长为")+QString::number(worktime)+tr("分钟"));
    ret=msgBox.exec();
    if(!ret){
        emit isReady(temper,humi,worktime);
        on_btnCancel_clicked();
    }
}

void ReadyWork::set_temper(float d)
{
    temper=d;
    ui->btnTemper->setText(QString::number(temper)+tr("℃"));
}

void ReadyWork::set_humi(float d)
{
    humi=d;
    ui->btnHumi->setText(QString::number(humi)+tr("%"));
}

void ReadyWork::set_worktime(U64 d)
{
    worktime=d;
    ui->btnTime->setText(QString::number(worktime)+tr("分钟"));
}
